#! /bin/bash

# Source function library.
. /etc/rc.d/init.d/functions

prog="zfs-fuse"
exec="/sbin/$prog"
config=/etc/sysconfig/$prog

[ -e $config ] && . $config

PIDFILE=/var/run/$prog.pid

unset LANG
ulimit -v unlimited
ulimit -c 512000
ulimit -l unlimited
ulimit -s unlimited

start() {
    [ -x $exec ] || (echo "$prog binary not present or executable" && exit 5)
    PID=`pidofproc $prog`
    start_status=$?
    case "$start_status" in
    0)
        echo "ZFS-FUSE is already running with pid $pid"
        exit 3
        ;;
    1)
        echo "Cleaning up stale $prog PID file in $PIDFILE"
        rm -f "$PIDFILE"
        ;;
    3)
        # not running
        ;;
    *)
    	echo "Huh?"
    	exit 99
    esac

    if [ "$ZFS_KILL_ORPHANS" == "yes_really" ] ; then
      echo -n Killing processes with unknown working directory:
      for a in 1 2 3 4 5 ; do
        orphans=`lsof -w -n | awk '$4 == "cwd" && $5 == "unknown" {print $2}'`
        echo -n $orphans
        [ "$orphans" == "" ] && break
        echo -n .
        kill $orphans
        sleep 1
      done
      orphans=`lsof -w -n | awk '$4 == "cwd" && $5 == "unknown" {print $2}'`
      if [ "$orphans" != "" ] ; then
        echo_failure ; echo
        echo -n Some orphans still live: $orphans  Killing with signal 9
        kill -9 $orphans
      fi
      orphans=`lsof -w -n | awk '$4 == "cwd" && $5 == "unknown" {print $2}'`
      if [ "$orphans" != "" ] ; then
        echo_failure ; echo
        echo -n Some orphans still live: $orphans
        echo_failure ; echo
        exit 8
      fi
      echo_success ; echo
    fi

    echo -n $"Starting $prog: "
    daemon $exec -p "$PIDFILE"
    exec_retval=$?
    echo
    [ $exec_retval -ne 0 ] && return $exec_retval
	
    for a in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ; do
        PID=`pidofproc $prog`
        [ "$PID" != "" ] && break
        echo -n "."
        sleep 1
    done

    if [ "$PID" = "" ] ; then
        echo "ZFS-FUSE did not start or create $PIDFILE"
        exit 3
    fi

    echo -n "Immunizing $prog against OOM kills"
    echo -17 > "/proc/$PID/oom_adj"
    ES_TO_REPORT=$?
    if [ "$ES_TO_REPORT" -ne 0 ] ; then
		echo_warning
        echo "code $ES_TO_REPORT"
        exit 3
    fi
    echo_success
    echo

	if [ "$ZFS_AUTOMOUNT" == "1" ] ; then
		echo "Deprecated use of ZFS_AUTOMOUNT option.  Use ZFS_AUTOMOUNT=yes instead."
		ZFS_AUTOMOUNT=yes
	fi

	if [ "$ZFS_AUTOMOUNT" == "yes" ] ; then
		echo -n $"Mounting zfs partitions: "
    	sleep 1
        rm -f /var/lib/random-seed
		zfs mount -a
		zfs_mount_retval=$?
		if [ $zfs_mount_retval = 0 ]; then
			echo_success
		else
			echo_warning
			echo zfs mount failed with code $zfs_mount_retval
		fi
		echo
    fi
    
    
#    if [ -x /nonexistent -a -x /usr/bin/renice ] ; then # DISABLED
#        log_action_begin_msg "Increasing ZFS-FUSE priority"
#        /usr/bin/renice -15 -g $PID > /dev/null
#        ES_TO_REPORT=$?
#        if [ 0 = "$ES_TO_REPORT" ] ; then
#            log_action_end_msg 0
#        else
#            log_action_end_msg 1 "code $ES_TO_REPORT"
#            exit 3
#        fi
#        true
#    fi 
        
    
    return $exec_retval
}

stop() {
    status_quiet || return 0
    [ -x $exec ] || (echo "$prog binary not present or executable" && exit 5)
    PID=`pidofproc $prog`
    if [ "$PID" != "" ] ; then
        echo -n "Syncing disks"
        sync
        echo_success
        echo

        echo -n "Unmounting ZFS filesystems"
        zfs unmount -a
        ES_TO_REPORT=$?
        if [ 0 = "$ES_TO_REPORT" ] ; then
            echo_success
        else
            echo_warning
        fi
        echo
    fi

    echo -n $"Stopping $prog: "
    killproc $prog
    kill_retval=$?
    echo
	
    if [ "$PID" != "" ] ; then
    	echo -n "Syncing disks again"
    	sync
    	echo_success
    	echo
    fi

    return $kill_retval
}

restart() {
	stop
	start
}

pool_status() {
    # run checks to determine if the service is running or use generic status
    status $prog && /sbin/zpool status
}

pool_status_quiet() {
    pool_status >/dev/null 2>&1
}

status_quiet() {
    status $prog  >/dev/null 2>&1
}

case "$1" in
    start)
        status_quiet && exit 0
        $1
        ;;
    stop)
        $1
        ;;
#    restart)
#    	restart 
#        ;;
#    reload)
#	restart
#        ;;
#    force-reload)
#	restart
#        ;;
#    status)
#        pool_status
#        ;;
#    condrestart|try-restart)
#        pool_status_quiet || exit 0
#        restart
#        ;;
    *)
#        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
